package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.DealParam;
import cn.net.shuxing.yifu.common.constant.UserParamConst;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import cn.net.shuxing.yifu.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

/**
 * 坐空Service
 */
@Service
public class ShortService {

    @Autowired
    private DealParamService dealParamService;

    @Autowired
    private DealParamKeyService dealParamKeyService;

    @Autowired
    private JedisUtil jedisUtil;

    /**
     * 获取用户剩余坐空id
     * @param userId
     * @return
     */
    public Integer getUserShortCount(Integer userId,Integer coinTypeId)
    {
        String shortCountStr = dealParamService.getValue(userId, coinTypeId,  UserParamConst.SHORT_COUNT);
        if (StringUtils.hasText(shortCountStr))
            return Integer.valueOf(shortCountStr);
        return 0;
    }

    public Integer getUserShortDeadline(Integer userId,Integer coinTypeId)
    {
        String shortDeadlineStr = dealParamService.getValue(userId, coinTypeId,UserParamConst.SHORT_DEADLINE);
        if (!StringUtils.hasText(shortDeadlineStr))
            return 0;
        try{
            return Integer.valueOf(shortDeadlineStr);
        }catch(Exception e){
            return 0;
        }
    }

    /**
     * 插入坐空
     * @param userId
     * @param coinTypeId
     * @param count
     * @param deadline
     */
    public boolean insertShort(Integer userId, Integer coinTypeId,Integer count, Integer deadline)
    {

        Integer shortCountKey = dealParamKeyService.getKeyByParamName(UserParamConst.SHORT_COUNT);
        String value = dealParamService.getValue(userId, coinTypeId, shortCountKey);
        if (!StringUtils.hasText(value))
        {
            dealParamService.insert(userId,coinTypeId,shortCountKey,String.valueOf(count));
        }
        else
            return false;

        Integer deadlineKey = dealParamKeyService.getKeyByParamName(UserParamConst.SHORT_DEADLINE);
        value = dealParamService.getValue(userId, coinTypeId, deadlineKey);
        if (!StringUtils.hasText(value))
        {
            dealParamService.insert(userId,coinTypeId,deadlineKey,String.valueOf(deadline));
        }
        else
            return false;
        return true;
    }

    public boolean updateShort(Integer userId, Integer coinTypeId,Integer count, Integer deadline)
    {
        Integer shortCountKey = dealParamKeyService.getKeyByParamName(UserParamConst.SHORT_COUNT);
        String value = dealParamService.getValue(userId, coinTypeId, shortCountKey);
        if (StringUtils.hasText(value))
            dealParamService.update(userId,coinTypeId,shortCountKey,String.valueOf(count));
        else
            return false;
        Integer deadlineKey = dealParamKeyService.getKeyByParamName(UserParamConst.SHORT_DEADLINE);
        value = dealParamService.getValue(userId, coinTypeId, deadlineKey);
        if (!StringUtils.hasText(value))
            dealParamService.update(userId,coinTypeId,deadlineKey,String.valueOf(deadline));
        else
            return false;
        return true;
    }
}
